package sample.clients;



import java.util.Scanner;

import org.hibernate.HibernateException;
import org.hibernate.Query;

import sample.dao.DAO;
import sample.entity.Advert;
import sample.entity.Category;

public class GetCategory  extends DAO{
	
	public static void main(String[] args) throws Exception {
		
		GetCategory self=new GetCategory();
		
		self.getCategory(1);
	
	}
	
	public void getCategory(long id) throws Exception{
		
		try{
			begin();
			
			Query categoryQuery=getSession().createQuery("from Category where id= :id");
			categoryQuery.setString("id", id+"");
			
			Category category=(Category)categoryQuery.uniqueResult();
			
			System.out.println(category.getId() +" , title: "+category.getTitle());
			
			System.out.println("Adverts in category :");
			
			
			// by default if object contains Set or Map - these collections are lazy loaded
			// ONLY when we NEED set or map from object , they are retriewed
			//(new Scanner(System.in)).nextLine();
			
			// here we acces to Set inside , so they are loaded
			for(Advert item :category.getAdverts()){
				System.out.println(item.getTitle());
			}
			commit();
			
		}
		catch(HibernateException e){
			throw new Exception(e.getMessage());
		}
		
	}
}
